<!DOCTYPE html>
<html lang="en-us" id="extr-page">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="../../global/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../global/layui/css/layui.css">
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
    <form class="form-horizontal" onsubmit="return false" id="form">
        <fieldset>
        <input type="hidden" id="id" name="id" />
          <div class="form-group">
           <label class="col-md-2 control-label">商城类型</label>
	           <div class="col-md-10">
	             <input class="form-control" placeholder="商城类型" type="text" name="type" id="type"  />
	           </div>
          </div>
           <div class="form-group">
           <label class="col-md-2 control-label">商品名称</label>
           <div class="col-md-10">
            <input class="form-control" placeholder=商品名称 type="text" name="goodsName" id="goodsName"  />
           </div>
          </div>
          <div class="form-group">
           <label class="col-md-2 control-label">商品原价</label>
           <div class="col-md-10">
            <input class="form-control" placeholder="商品原价" type="text" name="originalPrice" id="originalPrice"  />
           </div>
          </div>
          
          
            <div class="form-group">
                <label class="col-md-2 control-label">状态</label>
                <div class="col-md-10" style="width: 120px">
                 <select class="form-control input-sm" name="state" id="state">
                 <option value="0">未审核</option>
                 <option value="1" >已审核</option> 
                 </select>
            </div>
            </div>
          <div class="form-group">
           <label class="col-md-2 control-label" id="integrals">积分</label>
           <div class="col-md-10">
            <input class="form-control" placeholder="积分" type="text" name="integral" id="integral"  />
           </div>
          </div>
          <div class="form-group">
           <label class="col-md-2 control-label" id="redPackages">红包</label>
           <div class="col-md-10">
            <input class="form-control" placeholder="红包" type="text" name="redPackage" id="redPackage" />
           </div>
          </div>
          <div class="form-group">
           <label class="col-md-2 control-label" id="giftCertificates">礼品劵</label>
           <div class="col-md-10">
            <input class="form-control" placeholder="礼品劵" type="text" name="giftCertificate" id="giftCertificate" />
           </div>
          </div>
           <div class="form-group">
           <label class="col-md-2 control-label">库存</label>
           <div class="col-md-10">
            <input class="form-control" placeholder="库存" type="text" name="stock" id="stock" />
           </div>
          </div>
          
           <div class="form-group">
           <label class="col-md-2 control-label">排序</label>
           <div class="col-md-10">
            <input class="form-control" placeholder="排序" type="text" name="sort" id="sort" />
           </div>
          </div>
        
            <div class="form-actions">
                <div class="row" align="center">
                    <div class="col-md-12">
                        <button class="btn btn-primary" onclick="location.href='exchangeList.html'">返回</button>
                        <button class="btn btn-primary" type="submit" onclick="update()">
                            <i class="fa fa-save"></i> 保存
                        </button>
                    </div>
                </div>
            </div>

        </fieldset>
    </form>
</div>
<script type="text/javascript" src="../../global/js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../../global/js/jq.js"></script>
<script type="text/javascript" src="../../global/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
<script type="text/javascript" src="../../global/js/common.js"></script>
<script type="text/javascript" src="../../global/layui/layui.js"></script>
<script type="text/javascript">


    layui.use('layer', function () {
        var layer = layui.layer;
    });
    var id = getUrlParam("id");
        $.ajax({
            type: 'get',
            url:  '/clyjExchangeMall/getExchange/',
            contentType: "application/json; charset=utf-8",
            data:{id:id},
            async: false,
            success: function (data) {
            	$("#goodsName").val(data.goodsName);
            	$("#originalPrice").val(data.originalPrice);
            	
            }
        });
   
    initData();
    
    function initData() {
    	 var id = getUrlParam("id");
         if (id != "") {
             $.ajax({
                 type: 'get',
                 url: '/clyjExchangeMall/' + id,
                 async: false,
                 success: function (data) {
                	 if( $("#type").val()==0){
              
                		 $("#type").val("积分商城");
                		 $("#type").attr("disabled",true);
                		 $("#goodsName").attr("disabled",true);
                     	 $("#originalPrice").attr("disabled",true);
                     	 $("#redPackage").hide();
                         $("#giftCertificate").hide();
                         $("#redPackages").hide();
                         $("#giftCertificates").hide();
                	 }
                	 if( $("#type").val()==1){
                		 $("#type").val("红包商城");
                		 $("#type").attr("disabled",true);
                		 $("#goodsName").attr("disabled",true);
                     	 $("#originalPrice").attr("disabled",true);
                     	 $("#integral").hide();
                         $("#giftCertificate").hide();
                         $("#integrals").hide();
                         $("#giftCertificates").hide();
                	 }
                	 if( $("#type").val()==2){
                		 $("#type").val("礼品劵商城");
                		 $("#type").attr("disabled",true);
                		 $("#goodsName").attr("disabled",true);
                     	 $("#originalPrice").attr("disabled",true);
                     	 $("#integral").hide();
                         $("#redPackage").hide();
                         $("#integrals").hide();
                         $("#redPackages").hide();
                	 }
                	 $("#id").val(data.id);
                     $("#goodsId").val(data.goodsId);
                     $("#state").val(data.state); 
                     $("#integral").val(data.integral);
                     $("#redPackage").val(data.redPackage);
                     $("#giftCertificate").val(data.giftCertificate);
                     $("#stock").val(data.stock);
                     $("#sort").val(data.sort);
                 }
             });
         }
    }
    $('#form').bootstrapValidator();

    function update() {
        // 表单验证
        var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if (!bootstrapValidator.isValid()) {
            return;
        }

        var formdata = $("#form").serializeObject(  );

        $.ajax({
            type: 'put',
            url: '/clyjExchangeMall/updateExchange',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(formdata),
            success: function (data) {
            	console.log(data);
            	if(data.code=="success"){
            			layer.msg("修改成功", {shift: -1, time: 1000}, function () {
            				  location.href = "exchangeList.html"
                        });
            		
            	}else if(data.code=="fail"){
            		if(data.msg!=null){
            			layer.msg(data.msg, {shift: -1, time: 1000}, function () {
                            location.href = "exchangeList.html";
                        });
            		}else{
            			 location.href = "exchangeList.html";
            		}
            		
            	}
            }
        });
    }
</script>
</body>
</html>